package com.eax.CLILayer;
import java.util.Scanner;
import com.eax.ModelLayer.*;
import com.eax.ControlLayer.*;

public class AddressBookUI {
	private AddressController addressController;
	private DVDController dvdController;
	private ReservationController reservationController;
	private LendController lendController;


	//constructor which does nothing
	public AddressBookUI() {}

	public void start(AddressController addressController, DVDController dvdController, LendController lendController, ReservationController reservationController) {
		this.addressController = addressController;
		this.dvdController = dvdController;
		this.lendController = lendController;
		this.reservationController = reservationController;
		addressMenu();
	}

	public int writeAddressBookMenu() {
		Scanner keyboard = new Scanner(System.in);
		System.out.println("\f *** AddressBook Menu ***");
		System.out.println(" (1) Show information about person and his/her loans & reservations");
		System.out.println(" (2) Add person");
		System.out.println(" (3) Delete person");
		System.out.println(" (4) Update person");
		System.out.println(" (5) List all people");
		System.out.println(" (6) Return");
		System.out.print("\n Make your choice: ");

		return keyboard.nextInt();
	}

	public void addressMenu() {
		boolean exit = false;

		while(!exit) {
			int choice = writeAddressBookMenu();

			switch (choice) {
				case 1:
					findPerson();
					break;
				case 2:
					createPerson();
					break;
				case 3:
					deletePerson();
					break;
				case 4:
					updatePerson();
					break;
				case 5:
					listAllPeople();
					break;
				default:
					exit = true;
					break;
			}

		}
	}

	private int inputPerson(){
		Boolean ok = false;
		PersonModel person = null;
		String input;
		int id = 0;

		while(!ok){
			Scanner keyboard = new Scanner(System.in);
			System.out.print("Find person - Type search input: ");
			input = keyboard.nextLine();

			person = addressController.findPerson(input);

			if(person != null){
				id = person.getId();
				ok = true;
			} else {
				System.out.println("Person not found. Try again!");
			}
		}

		return id;
	}

	//the method reads id for a person and return
	public int inputPersonsID() {
		boolean ok = false;
		int id = 0;
		while(!ok) {
			// makes an object keyboard to have input from the console
			Scanner keyboard = new Scanner(System.in);
			System.out.print(" Write ID:  ");

			try {
				id = keyboard.nextInt();
				ok = true;
			} catch (Exception e) {
				System.out.println("A number needs to entered. Please try again!");
				String input = keyboard.nextLine();
			}
		}

		return id;
	}

	public String inputName() {
		// makes an object keyboard to have input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.print(" Write Name:  ");
		return keyboard.nextLine();
	}

	public int inputPostalCode() {
		// makes an object keyboard to have input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.print(" Write Postal Code:  ");
		return Integer.parseInt(keyboard.nextLine());
	}
	public String inputCity() {
		// makes an object keyboard to have input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.print(" Write City:  ");
		return keyboard.nextLine();
	}

	public int inputPhone() {
		// makes an object keyboard to have input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.print(" Write Phone Number:  ");
		return Integer.parseInt(keyboard.nextLine());
	}

	public String inputAddress() {
		// makes an object keyboard to read input from the console
		Scanner keyboard = new Scanner(System.in);
		System.out.print(" Write Address: ");
		return keyboard.nextLine();
	}

	// Print a persons ID.
	private void findPerson() {
		int id = inputPerson();

		System.out.println("Data on the Person: ");
		PersonModel p = addressController.getPerson(id);
		p.printPerson();

		System.out.println();
		System.out.println("DVD's "+p.getName()+" has rented or is renting: ");
		for(LendModel l : lendController.getAllLends()){
			if(l.getPersonId() == p.getId()){
				dvdController.findDVD(dvdController.getCopyBySerialNumber(l.getCopySerialNumber()).getDvdId()).printInfo();
				System.out.println("Lend info:");
				l.printInfo();
			}
		}
	}

	private void createPerson(){
		int id = addressController.getHighestId() + 1;
		String name = inputName();
		String address = inputAddress();
		int postalCode = inputPostalCode();
		String city = inputCity();
		int phoneNumber = inputPhone();

		addressController.createPerson(id, name, address, postalCode, city, phoneNumber);
		System.out.println(name + " has been added.\n");
	}

	private void deletePerson(){
		addressController.getPerson(inputPersonsID());
		System.out.println("Person has been deleted.");
	}

	private void updatePerson(){
		System.out.println("Not implemented yet.");
	}

	private void listAllPeople(){
		for(PersonModel p : addressController.listAllPersons()){
			p.printPerson();
			System.out.println();
		}
	}
}


